﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using System.IO;
using Ext.Net;
using Ext.Net.MVC;
using WebEntities;
using WebEntities.Models.WebInfo;
using com.subb.WebInfo;

namespace WebAdmin.Controllers
{
    [Authorize]
    public class HonorController : Controller
    {
        private const string HONOR_TILIMG_PATH = "HonorTilImgs/";
        private const string HONOR_ORIIMG_PATH = "HonorOriImgs/";

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetAllHonorByPage(StoreRequestParameters parms)
        {
            WI_HonorDAL hdal = new WI_HonorDAL();
            int ttlCount;
            List<WI_Honor> wihorlist = hdal.GetAllHonors(parms.Start, parms.Limit, out ttlCount);
            List<Honor> horList = new List<Honor>();
            foreach (var wihor in wihorlist)
            {
                Honor honor = Honor.AsHonor(wihor);
                horList.Add(honor);
            }
            return this.Store(horList, ttlCount);
        }

        [HttpPost]
        public ActionResult DeleteHonor(List<Honor> delHonors)
        {
            try
            {
                WI_HonorDAL hdal = new WI_HonorDAL();
                foreach (var honor in delHonors)
                {
                    hdal.RemoveHonor(honor.HonorID);
                }
                return this.Direct(new { Success = true, Msg = "删除成功！" });
            }
            catch (Exception ex)
            {
                return this.Direct(new { Success = false, Msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message });
            }
        }

        public ActionResult NewHonorWindow()
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "新建荣誉",
                Width = 700,
                Height = 300,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.TextListBullets,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("CreateHonor"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Render();
            return this.Direct();
        }

        public ActionResult CreateHonor()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateHonor(Honor vhonor)
        {
            FormPanelResult response = new FormPanelResult();
            response.IsUpload = true;
            try
            {
                string creator = Request.IsAuthenticated ? HttpContext.User.Identity.Name : "Anonymous";
                vhonor.Creator = creator;
                WI_HonorDAL hdal = new WI_HonorDAL();
                int horId=hdal.CreateHonor(vhonor);
                //上传图片
                if (Request.Files.Count > 0)
                {
                    HttpFileCollectionBase fb = Request.Files;
                    HttpPostedFileBase fpfb1 = fb.Get("TitleImg");
                    HttpPostedFileBase fpfb2 = fb.Get("OriginImg");
                    string savePath = string.Empty;
                    if (fpfb1 != null && fpfb1.ContentLength > 0)
                    {
                        string uploadVFolder = ConfigurationManager.AppSettings["UploadVFolder"];
                        if (!string.IsNullOrWhiteSpace(uploadVFolder))
                        {
                            savePath = Server.MapPath(uploadVFolder + HONOR_TILIMG_PATH);
                            if (!Directory.Exists(savePath))
                            {
                                Directory.CreateDirectory(savePath);
                            }
                            string extName = Path.GetExtension(fpfb1.FileName);
                            savePath += "htimg_" + horId + extName;
                            fpfb1.SaveAs(savePath);
                            //更新路径
                            string webpath = uploadVFolder + HONOR_TILIMG_PATH + "htimg_" + horId + extName;
                            Honor honor = Honor.AsHonor(hdal.GetHonor(horId));
                            honor.TitleImg = webpath;

                            //上传原始图
                            savePath = Server.MapPath(uploadVFolder + HONOR_ORIIMG_PATH);
                            if (!Directory.Exists(savePath))
                            {
                                Directory.CreateDirectory(savePath);
                            }
                            extName = Path.GetExtension(fpfb2.FileName);
                            savePath += "hoimg_" + horId + extName;
                            fpfb2.SaveAs(savePath);
                            webpath = uploadVFolder + HONOR_ORIIMG_PATH + "hoimg_" + horId + extName;
                            honor.OriginImg = webpath;
                            hdal.UpdateHonor(horId, honor);
                        }
                    }
                }
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "荣誉创建成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }

        public ActionResult EditHonorWindow(int horId)
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "编辑荣誉",
                Width = 700,
                Height = 300,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.TextListBullets,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("EditHonor"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Loader.Params.Add(new Parameter("horId", horId.ToString()));
            wnd.Render();
            return this.Direct();
        }

        public ActionResult EditHonor(int horId)
        {
            WI_HonorDAL hdal = new WI_HonorDAL();
            WI_Honor wihor = hdal.GetHonor(horId);
            Honor honor = Honor.AsHonor(wihor);
            return View(honor);
        }

        [HttpPost]
        public ActionResult EditHonor(Honor vhonor)
        {
            FormPanelResult response = new FormPanelResult();
            response.IsUpload = true;
            try
            {
                WI_HonorDAL ndal = new WI_HonorDAL();
                ndal.UpdateHonor(vhonor.HonorID, vhonor);
                int horId = vhonor.HonorID;
                //上传图片
                if (Request.Files.Count > 0)
                {
                    HttpFileCollectionBase fb = Request.Files;
                    HttpPostedFileBase fpfb1 = fb.Get("TitleImgFile");
                    HttpPostedFileBase fpfb2 = fb.Get("OriginImgFile");
                    string savePath = string.Empty;
                    if (fpfb1 != null && fpfb1.ContentLength > 0)
                    {
                        string uploadVFolder = ConfigurationManager.AppSettings["UploadVFolder"];
                        if (!string.IsNullOrWhiteSpace(uploadVFolder))
                        {
                            savePath = Server.MapPath(uploadVFolder + HONOR_TILIMG_PATH);
                            if (!Directory.Exists(savePath))
                            {
                                Directory.CreateDirectory(savePath);
                            }
                            string extName = Path.GetExtension(fpfb1.FileName);
                            savePath += "htimg_" + horId + extName;
                            fpfb1.SaveAs(savePath);
                            //更新路径
                            string webpath = uploadVFolder + HONOR_TILIMG_PATH + "htimg_" + horId + extName;
                            Honor honor = Honor.AsHonor(ndal.GetHonor(horId));
                            honor.TitleImg = webpath;

                            //上传原始图
                            savePath = Server.MapPath(uploadVFolder + HONOR_ORIIMG_PATH);
                            if (!Directory.Exists(savePath))
                            {
                                Directory.CreateDirectory(savePath);
                            }
                            extName = Path.GetExtension(fpfb2.FileName);
                            savePath += "hoimg_" + horId + extName;
                            fpfb2.SaveAs(savePath);
                            webpath = uploadVFolder + HONOR_ORIIMG_PATH + "hoimg_" + horId + extName;
                            honor.OriginImg = webpath;
                            ndal.UpdateHonor(horId, honor);
                        }
                    }
                }
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "荣誉更新成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }
    }
}
